<?php

/* ******************************
 *	Request aJax actions
* ******************************
*/

require_once('../../includes/classes/core.php');
include('../../includes/classes/log.class.php');
$log 		= new log();
$action 	= $_REQUEST['act'];
$error		= '';
$data		= '';

//incomming
$incom_id				= $_REQUEST['id'];
$call_date				= $_REQUEST['call_date'];
$site_user_pin			= $_REQUEST['pin'];
$call_type_id			= $_REQUEST['call_type_id'];
$phone					= $_REQUEST['phone'];
$category_id			= $_REQUEST['category_id'];
$problem_date			= $_REQUEST['problem_date'];
$call_content			= $_REQUEST['call_content'];
$category_parent_id 	= $_REQUEST['category_parent_id'];
$call_status_id			= $_REQUEST['call_status_id'];

$pay_type_id			= $_REQUEST['pay_type_id'];
$bank_id				= $_REQUEST['bank_id'];
$card_type_id			= $_REQUEST['card_type_id'];
$pay_aparat_id			= $_REQUEST['pay_aparat_id'];
$object_id				= $_REQUEST['object_id'];
$source_id				= $_REQUEST['source_id'];


// site_user
$personal_pin			= $_REQUEST['personal_pin'];
$personal_id			= $_REQUEST['personal_id'];


//task
$persons_id			    = $_REQUEST['persons_id'];
$task_type_id			= $_REQUEST['task_type_id'];
$priority_id			= $_REQUEST['priority_id'];
$comment 	        	= $_REQUEST['comment'];
$task_department_id 	= $_REQUEST['task_department_id'];
$hidden_inc				= $_REQUEST['hidden_inc'];
$edit_id				= $_REQUEST['edit_id'];
$delete_id				= $_REQUEST['delete_id'];

// file
$rand_file				= $_REQUEST['rand_file'];
$file					= $_REQUEST['file_name'];


switch ($action) {
	case 'get_add_page':
		$number		= $_REQUEST['number'];
		$page		= GetPage($res='', $number);
		$data		= array('page'	=> $page);

		break;
	case 'disable':
		$incom_id				= $_REQUEST['id'];
		mysql_query("			UPDATE `incomming_call`
									    SET `actived`=0
										WHERE `id`=$incom_id ");
		break;
	case 'get_edit_page':
		$page		= GetPage(Getincomming($incom_id));

		$data		= array('page'	=> $page);

		break;
	case 'get_list' :
		$count = 		$_REQUEST['count'];
		$hidden = 		$_REQUEST['hidden'];
		mysql_query("SET @i = 0;");
	  	$rResult = mysql_query("SELECT  		incomming_call.id,    
    											incomming_call.id, 
    											DATE_FORMAT(incomming_call.`date`,'%d/%m/%y %H:%i:%s') AS `date`,
    											incomming_call.`pin`,
    											incomming_call.`pid`,
    											incomming_call.phone,
    											category.`name`,
    											`user1`.`name` ,
    											`person2`.`name` ,
    											if(ISNULL(task.incomming_call_id), 'მოგვარებულია', `status`.`call_status`)AS `status`,				
    											TIME_FORMAT(SEC_TO_TIME(asterisk_incomming.duration), '%i:%s') AS call_time,
    											CONCAT('<button class=\'download\' str=',asterisk_incomming.file_name,'>მოსმენა</button>') AS `file`
                                FROM 			incomming_call
                                LEFT JOIN 		category  ON incomming_call.call_category_id=category.id
                                LEFT JOIN 		task ON task.incomming_call_id=incomming_call.id								
                                LEFT JOIN 		persons AS `user1`			ON task.responsible_user_id=user1.id	
                                LEFT JOIN 		users   AS `user2`			ON incomming_call.user_id=user2.id
                                LEFT JOIN 		persons AS `person2`		ON user2.person_id=person2.id
                                LEFT JOIN 		`status` ON task.`status`=`status`.id
                                LEFT JOIN 		asterisk_incomming ON DATE(asterisk_incomming.call_datetime) = DATE(incomming_call.`date`) AND asterisk_incomming.source = incomming_call.phone
                                
                                WHERE 			incomming_call.actived = 1 AND DATE(incomming_call.date)=CURDATE()
                                GROUP BY 	    incomming_call.id");
	  
		$data = array(
				"aaData"	=> array()
		);

while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			$row1 = array();
			
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				
				if($i==9){
						 if 	($aRow[$i]=='მოგვარებულია') 			{	$row[] = '<div style="background: #80FF54; WIDTH: 150%;">'.$aRow[$i].'</div>';}
						 elseif ($aRow[$i]=='გარკვევის პროცესშია') 	{	$row[] = '<div style="background:#FFFF55; WIDTH: 150%;">'.$aRow[$i].'</div>';}
						 elseif ($aRow[$i]=='გადაცემულია გასარკვევად') 	{	$row[] = '<div style="background: #FF5555; WIDTH: 150%;">'.$aRow[$i].'</div>';}
						 else	{ $row[] = $aRow[$i];}	
				
				}else	{ $row[] = $aRow[$i];}
			}
			$data['aaData'][] = $row;
		}
		
		break;
	
	
				case 'up_now':
				$user		= $_SESSION['USERID'];
				if($rand_file != ''){
				mysql_query("INSERT INTO 	`file`
				( 	`user_id`,
				`incomming_call_id`,
				`name`,
				`rand_name`
				)
				VALUES
				(	'$user',
				'$edit_id',
				'$file',
				'$rand_file'
				);");
				}
		
				$increm = mysql_query("	SELECT  `name`,
				`rand_name`,
				`id`
				FROM 	`file`
				WHERE   `incomming_call_id` = $edit_id
				");
		
				$data1 = '';
		
				while($increm_row = mysql_fetch_assoc($increm))	{
				$data1 .='<tr style="border-bottom: 1px solid #85b1de;">
				<td style="width:110px; display:block;word-wrap:break-word;">'.$increm_row[name].'</td>
				<td ><button type="button" value="media/uploads/file/'.$increm_row[rand_name].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none;background-image:url(\'media/images/get.png\');" id="download" ></button><input type="text" style="display:none;" id="download_name" value="'.$increm_row[rand_name].'"> </td>
				          <td ><button type="button" value="'.$increm_row[id].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none; background-image:url(\'media/images/x.png\');" id="delete"></button></td>
						          </tr>';
		}
		
		$data = array('page' => $data1);
		
		break;
		
	case 'sub_category':
		
		$cat_id	=	$_REQUEST['cat_id'];
		$data 	= 	array('cat'=>Getcategory1($cat_id));
		
		break;	
	case 'set_task_type':
	
		$cat_id	=	$_REQUEST['cat_id'];
		$data 	= 	array('cat'=>Getbank_object($cat_id));
	
		break;
	default:
		$error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	Request Functions
* ******************************
*/



function Getcall_status($status)
{
	$data = '';
	$req = mysql_query("SELECT 	`id`, `call_status`
						FROM 	`status`
						WHERE 	actived=1");
	

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		
		if($res['id'] == $status){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['call_status'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['call_status'] . '</option>';
		}
	}
	return $data;
}
	function Getpay_type($pay_type_id)
	{
		$data = '';
		$req = mysql_query("SELECT 	`id`, `name`
						FROM 	`pay_type`
						WHERE 	actived=1");
	
	
		$data .= '<option value="0" selected="selected">----</option>';
		while( $res = mysql_fetch_assoc($req)){
			if($res['id'] == $pay_type_id){
				$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
			} else {
				$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
			}
		}
	
		return $data;
	}
	function Get_bank($bank_id)
	{
		$data = '';
		$req = mysql_query("SELECT 	`id`, `name`
						FROM 	`bank`
						WHERE 	actived=1");
	
	
		$data .= '<option value="0" selected="selected">----</option>';
		while( $res = mysql_fetch_assoc($req)){
			if($res['id'] == $bank_id){
				$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
			} else {
				$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
			}
		}
	
		return $data;
	}
	
function Getcard_type($card_type_id)
{
	$data = '';
	$req = mysql_query("SELECT 	`id`, `name`
					FROM 	`card_type`
					WHERE 	actived=1");


	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $card_type_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}
function Getpay_aparat($pay_aparat_id)
{
	$data = '';
	$req = mysql_query("SELECT 	`id`, `name`
					FROM 	`pay_aparat`
					WHERE 	actived=1");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $pay_aparat_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}
function Getobject($object_id)
{
	$data = '';
	$req = mysql_query("SELECT 	`id`, `name`
					FROM 	`object`
					WHERE 	actived=1");


	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $object_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function Getsource($object_id)
{
	$data = '';
	$req = mysql_query("SELECT 	`id`, `name`
					FROM 	`source`
					WHERE 	actived=1");


	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $object_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}
function Getcategory($category_id)

{ 			

	$data = '';
	$req = mysql_query("SELECT `id`, `name`
						FROM `category`
						WHERE actived=1 && parent_id=0 ");


	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $category_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function Getcategory1($category_id)
{

	$data = '';
	$req = mysql_query("SELECT `id`, `name`
						FROM `category`
						WHERE actived=1 && parent_id=$category_id");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $category_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
	
}
function Get_control($persons_id)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
							FROM `crystal_users`
							WHERE actived=1 ");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $persons_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}
function Getcategory1_edit($category_id)
{

	$data = '';
	$req = mysql_query("SELECT `id`, `name`
						FROM `category`
						WHERE actived=1 && id=$category_id");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $category_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;

}

function Getcall_type($call_type_id)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
					FROM `call_type`
					WHERE actived=1");
	
	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $call_type_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}


function Getdepartment($task_department_id)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
					    FROM `department`
					    WHERE actived=1 ");
	

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $task_department_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}


function Getpriority($priority_id)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
						FROM `priority`
						WHERE actived=1 ");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $priority_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function Gettask_type($task_type_id)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
					    FROM `task_type`
					    WHERE actived=1 ");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $task_type_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}


function Getpersons($persons_id)
{
	$data = '';
	$req = mysql_query("SELECT `id`, `name`
							FROM `crystal_users`
							WHERE actived=1");

	$data .= '<option value="0" selected="selected">----</option>';
	while( $res = mysql_fetch_assoc($req)){
		if($res['id'] == $persons_id){
			$data .= '<option value="' . $res['id'] . '" selected="selected">' . $res['name'] . '</option>';
		} else {
			$data .= '<option value="' . $res['id'] . '">' . $res['name'] . '</option>';
		}
	}

	return $data;
}

function Getincomming($incom_id)
{
$res = mysql_fetch_assoc(mysql_query("SELECT    	incomming_call.id AS id,
													incomming_call.phone AS `phone`,
													incomming_call.uniqueid as inc_uniqueid,
													task.uniqueid as task_uniqueid,
													crystal_users.`name` AS operator,
													incomming_call.user_id AS `user_id`,
													DATE_FORMAT(incomming_call.`date`,'%d/%m/%y %H:%i:%s') AS call_date,
													DATE_FORMAT(incomming_call.`date`,'%Y-%m-%d %H') AS record_date,
													incomming_call.call_type_id AS call_type_id,
													incomming_call.call_category_id AS category_id,
													IF(ISNULL(task.`status`), 2, task.`status`) AS `status`,
													incomming_call.call_subcategory_id AS category_parent_id,
													DATE_FORMAT(incomming_call.`problem_date`,'%d/%m/%y %H:%i:%s') AS problem_date,
													incomming_call.call_content AS call_content,
													incomming_call.pay_type_id AS pay_type_id,
													incomming_call.bank_id AS bank_id,
													incomming_call.bank_object_id AS bank_object_id,
													incomming_call.card_type_id AS card_type_id,
													incomming_call.card_type_id AS card_type1_id,
													incomming_call.pay_aparat_id AS pay_aparat_id,
													incomming_call.object_id AS object_id,
													incomming_call.source_id,
													incomming_call.pin AS personal_pin,
													incomming_call.pid AS personal_id,
													task.task_type_id AS task_type_id,
													task.responsible_user_id AS persons_id,
													task.priority_id AS priority_id,
													task.control_user_id,
													task.problem_comment,
													task.phone as task_phone,
													task.department_id AS task_department_id
										FROM 	 incomming_call
										LEFT JOIN  	task  ON incomming_call.id=task.incomming_call_id
										LEFT JOIN crystal_users ON crystal_users.id=incomming_call.user_id
										
										where      	incomming_call.id =$incom_id
			" ));
	
	return $res;
}


function GetPage($res='', $number)
{
	$num = 0;
	if($res[phone]==""){
		$num=$number;
	}else{ 
		$num=$res[phone]; 
	}
	if ($res[id]!='') {
		
		$dis='disabled="disabled"';
	}else{
		$dis='';
	}

	$increm = mysql_query("	SELECT  `name`,
									`rand_name`
							FROM 	`file`
							WHERE   `incomming_call_id` = $res[id]
			");
	
	$data  .= '
	<!-- jQuery Dialog -->
    <div id="add-edit-goods-form" title="საქონელი">
    	<!-- aJax -->
	</div>
	<div id="dialog-form">
			<div style="float: left; width: 673px;">	
				<fieldset >
			    	<legend>ძირითადი ინფორმაცია</legend>
						<tr>
							<label for="req_num">დამფორმირებელი</label>
							<label for="req_num">:</label>
							<label for="req_num">' .  $res['operator']. '</label>
						</tr>
			    	<table width="100%" class="dialog-form-table">
						<tr>
							<td style="width: 180px;"><label for="req_num">მომართვა №</label></td>
							<td style="width: 180px !important;"><label for="req_data">თარიღი</label></td>
							<td style="width: 180px;"><label for="req_phone">ტელეფონი</label></td>
						</tr>
						<tr>
							<td style="width: 180px;">
								<input type="text" id="id" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField user_id\'" value="' . $res['id']. '" disabled="disabled" />
							</td>
							<td style="width: 180px;">
								<input type="text" id="c_date" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField date\'" value="' .  $res['call_date']. '" disabled="disabled" />
							</td>
							<td style="width: 180px;">
								<input type="text" id="phone" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $num . '" disabled="disabled"/>
							</td>
							<td style="width: 69px;">
								
							</td>
						</tr>
						<tr>
							<td style="width: 180px;"><label for="d_number">ზარის ტიპი</label></td>
							<td style="width: 180px;"><label for="d_number">კატეგორია</label></td>
							<td style="width: 180px;"><label for="d_number">ქვე-კატეგოტია</label></td>
							<td ></td>
						</tr>
						<tr>
							<td style="width: 180px;"><select id="call_type_id" class="idls object"disabled="disabled">'. Getcall_type($res['call_type_id']).'</select></td>
							<td style="width: 180px;"><select id="category_parent_id" class="idls object"disabled="disabled">'. Getcategory($res['category_parent_id']).'</select></td>
							<td style="width: 180px;"><select id="category_id" class="idls object"disabled="disabled">'. Getcategory1_edit($res['category_id']).'</select></td>
							<td ></td>
						</tr>
					</table>';
										
		$data  .= '<table id="additional" class="hidden dialog-form-table" width="100%">

						<tr id="aaaa">
							<td style="width: 180px;"><label pey_label1 for="d_number">ტრანზაქციის ფორმა</label></td>
							<td style="width: 180px;"><label id="bank_label" for="d_number">მომსახურე ბანკი</label></td>
							<td style="width: 180px;"><label id="pey_label" for="d_number">აპარატის ტიპი</label></td>
							<td style="width: 106px;"></td>
						</tr>
						<tr id="c">
							<td style="width: 180px;"><select id="pay_type_id" class="idls object"disabled="disabled">'. Getpay_type($res['pay_type_id']).'</select></td>
							<td style="width: 180px;"><select id="bank_id" class="idls object"disabled="disabled">'. Get_bank($res['bank_id']).'</select></td>
							<td style="width: 180px;"><select id="pay_aparat_id" class="idls object"disabled="disabled">'. Getpay_aparat($res['pay_aparat_id']).'</select></td>
							<td style="width: 106px;"></td>
						</tr>
						<tr id="bbbb">
							<td style="width: 180px;"><label id="card_label" for="d_number">ბარათის ტიპი</label></td>
							<td style="width: 180px;"></td>
							<td style="width: 180px;"></td>
							<td style="width: 106px;"></td>
						</tr>
						<tr id="d" >
							<td style="width: 180px;"><select id="card_type_id" class="idls object"disabled="disabled">'. Getcard_type($res['card_type_id']).'</select></td>
							<td style="width: 180px;"></td>
							<td style="width: 180px;"></td>
							<td style="width: 106px;"></td>
						</tr>
					</table>';
												
		$data  .= '<table width="100%" class="dialog-form-table">
						<tr>
							<td style="width: 180px;"><label for="req_num">პრობლემის თარიღი</label></td>
							<td style="width: 180px;"><label for="d_number">სტატუსი</label></td>
							<td style="width: 180px;"><label for="d_number">ობიექტი</label></td>			
							<td ></td>
						</tr>
						<tr>	
							<td style="width: 180px;"> <input type="text" id="problem_date" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['problem_date'] . '"disabled="disabled" /></td>
							<td style="width: 180px;"><select id="call_status_id" class="idls object"disabled="disabled"disabled="disabled">'. Getcall_status($res['status']).'</select></td>
							<td style="width: 180px;"><select id="object_id" class="idls object"disabled="disabled">'. Getobject($res['object_id']).'</select></td>	
							<td ></td>
						</tr>
						
						<tr>
							<td style="width: 180px;"><label for="req_num">ინფორმაციის წყარო</label></td>
							<td style="width: 180px;"></td>
							<td style="width: 180px;"></td>			
							<td ></td>
						</tr>
						<tr>	
							<td style="width: 180px;"><select id="source_id" class="idls object"disabled="disabled">'. Getsource($res['source_id']).'</select></td>
							<td style="width: 180px;"></td>
							<td style="width: 180px;"></td>	
							<td ></td>
						</tr>
						<tr>
							<td style="width: 180px;"><label for="content">საუბრის შინაარსი</label></td>
						</tr>
						<tr>
							
							<td colspan="6">	
								<textarea  style="width: 641px; resize: none;" id="call_content" class="idle" name="call_content" cols="300" rows="4"disabled="disabled">' . $res['call_content'] . '</textarea>
							</td>
						</tr>			
					</table>
				</fieldset >
		   
				<fieldset style="margin-top: 5px;">
			    	<legend>დავალების ფორმირება</legend>
		
			    	<table class="dialog-form-table">
						<tr>
							<td style="width: 180px;"><label for="d_number">დავალების ტიპი</label></td>
							<td style="width: 180px;"><label for="d_number">განყოფილება</label></td>
							<td style="width: 180px;"><label for="d_number">პასუხისმგებელი პირი</label></td>
						</tr>
			    		<tr>
							<td style="width: 180px;" id="task_type_change"><select id="task_type_id" class="idls object"disabled="disabled">'.Gettask_type($res['task_type_id']).'</select></td>
							<td style="width: 180px;"><select id="task_department_id" class="idls object"disabled="disabled">'. Getdepartment($res['task_department_id']).'</select></td>
							<td style="width: 180px;"><select id="persons_id" class="idls object"disabled="disabled">'.Getpersons($res['persons_id']).'</select></td>
						</tr>
						<tr>
							<td style="width: 180px;"><label for="d_number">პრიორიტეტები</label></td>
							<td style="width: 180px;"><label for="d_number">მაკონტროლებელი</label></td>
							<td style="width: 180px;"><label for="d_number">სხვა ნომერი</label></td>
						</tr>
						<tr>
							<td style="width: 180px;"><select id="priority_id" class="idls object">'.Getpriority($res['priority_id']).'</select></td>
							<td style="width: 180px;"><select id="controler_id" class="idls object">'.Get_control($res['control_user_id']).'</select></td>
							<td style="width: 180px;">
								<input type="text" id="task_phone" class="idle" onblur="this.className=\'idle\'" value="' .  $res['task_phone']. '" />	
							</td>
						</tr>
						<tr>
							<td style="width: 180px;"><label for="content">პრობლემის გადაწყვეტა</label></td>
						</tr>
						<tr>
							
							<td colspan="6">	
								<textarea  style="width: 641px; resize: none;" id="problem_coment" class="idle" name="call_content" cols="300" rows="4"disabled="disabled">' . $res['problem_comment'] . '</textarea>
							</td>
						</tr>	
					</table>
		        </fieldset>
			</div>
			<div>
				  </fieldset>
			</div>
			<div style="float: right;  width: 355px;">
				 <fieldset>
					<legend>მომართვის ავტორი</legend>
					<table style="height: 119px;">
						<tr>
							<td style="width: 180px;">PIN კოდი</td>
							<td style="width: 180px;"></td>
						</tr>
						<tr>
							<td style="width: 180px;"><input type="text" id="personal_pin" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['personal_pin']  . '"'.$dis.' disabled="disabled"/></td>
							<td style="width: 180px;" class="hidden friend">
							</td>
						</tr>
						<tr>
							<td style="width: 180px;">პირადი ნომერი</td>
							<td style="width: 180px;"></td>
						</tr>
						<tr>
							<td style="width: 180px;">
								<input type="text" id="personal_id" class="idle" onblur="this.className=\'idle\'" onfocus="this.className=\'activeField\'" value="' . $res['personal_id'] . '" '.$dis.'disabled="disabled"/>
							</td>
							<td style="width: 180px;"></td>
						</tr>
					</table>
				</fieldset>
				<div id="additional_info">';
					
	  $data .= '</div>';
	  
	  		$data .= GetRecordingsSection1($res);
	  		$data .= GetRecordingsSection($res);
	  		
		$data .= '		<fieldset>
					<legend>დამატებითი ინფორმაცია</legend> 
					<table style="float: right; border: 1px solid #85b1de; width: 150px; text-align: center;">
					<tr>
						<td>
							<div class="file-uploader">
								<input id="choose_file" type="file" name="choose_file" class="input" style="display: none;">
								<button id="choose_button" class="center">აირჩიეთ ფაილი</button>
								<input id="hidden_inc" type="text" value="'. increment('incomming_call') .'" style="display: none;">
							</div>
						</td>
					</tr>
				</table>
			     <table style="float: right; border: 1px solid #85b1de; width: 150px; text-align: center;">
			          <tr style="border-bottom: 1px solid #85b1de;">
			           <td colspan="3">მიმაგრებული ფაილი</td>
			          </tr>
				</table>
				<table id="file_div" style="float: right; border: 1px solid #85b1de; width: 150px; text-align: center;">';
					
					while($increm_row = mysql_fetch_assoc($increm))	{	
						$data .=' 
								        <tr style="border-bottom: 1px solid #85b1de;">
								          <td style="width:110px; display:block;word-wrap:break-word;">'.$increm_row[name].'</td>													 
								          <td ><button type="button" value="media/uploads/file/'.$increm_row[rand_name].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none;background-image:url(\'media/images/get.png\');" id="download" ></button><input type="text" style="display:none;" id="download_name" value="'.$increm_row[rand_name].'"> </td>
								          <td ><button type="button" value="'.$increm_row[id].'" style="cursor:pointer; border:none; margin-top:25%; display:block; height:16px; width:16px; background:none; background-image:url(\'media/images/x.png\');" id="delete"></button></td>
								        </tr>';
					}
			         
	 $data .= '
	 		</table>
				</fieldset>
			</div>
	 		<input id="hidden_user" type="text" value="'. $res['user_id'] .'" style="display: none;">
    </div>';

	return $data;
}

function GetRecordingsSection($res)
{
	if ($res[phone]==''){
		//$data .= '<td colspan="2" style="height: 20px; text-align: center;">ჩანაწერები ვერ მოიძებნა</td>';
	}else{
	$req = mysql_query("SELECT  	DATE_FORMAT(asterisk_incomming.call_datetime,'%d/%m/%y %H:%i:%s') AS `time`,
									asterisk_incomming.file_name AS `userfield`
						FROM 		asterisk_incomming
						WHERE		asterisk_incomming.uniqueid ='$res[inc_uniqueid]'
						");
	}
	$data .= '
        <fieldset style="margin-top: 0px; width: 155px; float: right;">
            <legend>შემომავალი ზარი</legend>

            <table style="width: 65%; border: solid 1px #85b1de; margin:auto;">
                <tr style="border-bottom: solid 1px #85b1de; height: 20px;">
                    <th style="padding-left: 10px;">დრო</th>
                    <th  style="border: solid 1px #85b1de; padding-left: 10px;">ჩანაწერი</th>
                </tr>';
	if (mysql_num_rows($req) == 0){
		$data .= '<td colspan="2" style="height: 20px; text-align: center;">ჩანაწერები ვერ მოიძებნა</td>';
	}
	
	while( $res2 = mysql_fetch_assoc($req)){
		$src = $res2['userfield'];
		$data .= '
                <tr style="border-bottom: solid 1px #85b1de; height: 20px;">
                    <td>' . $res2['time'] . '</td>
                    <td><button class="download" str="' . $src . '">მოსმენა</button></td>
                </tr>';
	}

	$data .= '
            </table>
        </fieldset>';
	
	return $data;
}
function GetRecordingsSection1($res)
{
	if ($res[phone]==''){
		//$data .= '<td colspan="2" style="height: 20px; text-align: center;">ჩანაწერები ვერ მოიძებნა</td>';
	}else{
		$req = mysql_query("SELECT  DATE_FORMAT(asterisk_outgoing.call_datetime,'%d/%m/%y %H:%i:%s') AS `time`,
									asterisk_outgoing.file_name AS `userfield`
							FROM 	asterisk_outgoing
							WHERE 	asterisk_outgoing.uniqueid='$res[task_uniqueid]'");
	}
	$data .= '
        <fieldset style="margin-top: 0px; width: 154px; float: right;">
            <legend>გამავალი ზარი</legend>

            <table style="width: 65%; border: solid 1px #85b1de; margin:auto;">
                <tr style="border-bottom: solid 1px #85b1de; height: 20px;">
                    <th style="padding-left: 10px;">დრო</th>
                    <th  style="border: solid 1px #85b1de; padding-left: 10px;">ჩანაწერი</th>
                </tr>';
	if (mysql_num_rows($req) == 0){
		$data .= '<td colspan="2" style="height: 20px; text-align: center;">ჩანაწერები ვერ მოიძებნა</td>';
	}

	while( $res2 = mysql_fetch_assoc($req)){
		$src = $res2['userfield'];
		$data .= '
                <tr style="border-bottom: solid 1px #85b1de; height: 20px;">
                    <td>' . $res2['time'] . '</td>
                    <td><button class="download1" str="' . $src . '">მოსმენა</button></td>
                </tr>';
	}

	$data .= '
            </table>
        </fieldset>';

	return $data;
}
function increment($table){

	$result   		= mysql_query("SHOW TABLE STATUS LIKE '$table'");
	$row   			= mysql_fetch_array($result);
	$increment   	= $row['Auto_increment'];
	$next_increment = $increment+1;
	mysql_query("ALTER TABLE '$table' AUTO_INCREMENT=$next_increment");

	return $increment;
}

?>